/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
package agents;

import jade.core.AID;
import jade.core.Agent;
import jade.core.behaviours.CyclicBehaviour;
import jade.core.behaviours.TickerBehaviour;
import jade.lang.acl.ACLMessage;
import jade.lang.acl.MessageTemplate;
import java.awt.Point;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.Map.Entry;
import libPackage.CommonFunction;
import libPackage.ConstHelper;

/**
 *
 * @author vaduc
 */

public class Herbivore extends Animal {

//    protected HashMap<AID, Point> targets;
    private boolean isEating;

    @Override
    protected void setup() {
        super.setup();
        commonFunction = new CommonFunction();
        commonFunction.registerService(this, "herbivore");
        targetType="plante";
        eatDistance=ConstHelper.DISTANCE_EAT_HERBIVORE;
    }

    protected class GetEatResponse extends CyclicBehaviour {
        @Override
        public void action() {
            MessageTemplate messageTemplate=
                    MessageTemplate.and(MessageTemplate.MatchPerformative(ACLMessage.INFORM), 
                            MessageTemplate.MatchOntology("ate"));                
            ACLMessage receiveMessage=receive(messageTemplate);
            if(receiveMessage!=null){
                energie += ConstHelper.EAT_TRANFORM_ENERGIE;
                
            }else{
                block();
            }              
        }
    }
//    private void addBehaviours() {
//        addBehaviour(new GetAllTarget(this, 5000));
//        addBehaviour(new GetTargetPosition());
//        addBehaviour(new Eating(this, 1000));
//        addBehaviour(new SetTarget(this, 2000));
//    }

//    protected class GetAllTarget extends TickerBehaviour {
//
//        // Send message to all tree for accquiring tree position
//        // PreCon: Agent is hungry
//
//        public GetAllTarget(Agent a, long period) {
//            super(a, period);
//        }
//        
//        @Override
//        public void onTick() {
//            if (energie < ConstHelper.ENERGIE_HUNGRY) {
//                ArrayList<AID> plantes = commonFunction.getAllPlantes(myAgent);
//                ACLMessage messageReponse = new ACLMessage(ACLMessage.CFP);
//                messageReponse.setOntology("position");
//                for (AID plante : plantes) {
//                    messageReponse.addReceiver(plante);
//                    messageReponse.setContent(ConstHelper.REQUEST_POSITION_MESSAGE);
//                    send(messageReponse);
//                }
//            } else {
//                block();
//            }
//        }
//    }
//
//    protected class GetTargetPosition extends CyclicBehaviour {
//
//        // Receive all response messages of all trees
//        @Override
//        public void action() {
//            MessageTemplate messageTemplate
//                    = MessageTemplate.and(MessageTemplate.MatchPerformative(ACLMessage.INFORM),
//                            MessageTemplate.MatchOntology("position"));
//            ACLMessage receiveMessage = receive(messageTemplate);
//            if (receiveMessage != null) {
//                String[] messageContent = receiveMessage.getContent().split(" ");
//                int x = Integer.parseInt(messageContent[0]);
//                int y = Integer.parseInt(messageContent[1]);
//                Point point = new Point(x, y);
//                targets.put(receiveMessage.getSender(), point);
//            } else {
//                block();
//            }
//        }
//
//    }
//
//    protected class Eating extends TickerBehaviour {
//
//        public Eating(Agent a, long period) {
//            super(a, period);
//        }
//
//        @Override
//        public void onTick() {
//            if ((position.x == destination.x)&&(position.y == destination.y)
//                    && (target != null)) {               
//                ACLMessage messageEat = new ACLMessage(ACLMessage.CFP);  
//                messageEat.setOntology("eat");                
//                messageEat.addReceiver(target);                
//                messageEat.setContent("You are ate");            
//                send(messageEat);
//                System.out.println("Eating, energie: "+energie);
//                energie += ConstHelper.EAT_TRANFORM_ENERGIE;
//                if (energie >= ConstHelper.ENERGIE_MAX){
//                    target = null;
//                    destination = new Point(random.nextInt(100), random.nextInt(100));
//                }
//                 
//            } else {
//                block();
//            }
//        }
//    }
//
//    private class SetTarget extends TickerBehaviour {
//
//        // Set target(destination) to a tree
//        // PreCon: Agent is hungry
//
//        public SetTarget(Agent a, long period) {
//            super(a, period);
//        }
//        
//        @Override
//        public void onTick() {
//            if ((energie < ConstHelper.ENERGIE_HUNGRY)
//                    && (targets.size() > 0)
//                    && (target == null)) {
//                target = GetNearestTarget(targets);
//                destination = targets.get(target);
//            } else {
//                block();
//            }
//        }
//    }
//
//    private AID GetNearestTarget(HashMap<AID, Point> trees) {
//        if (trees.size() <= 0) {
//            return null;
//        }
//        AID nearestTarget = new AID();
//        double smallestDistance = Double.POSITIVE_INFINITY;
//        for (Entry<AID, Point> entry : trees.entrySet()) {
//            double currentDistance
//                    = commonFunction.getDistance(this.position, entry.getValue());
//            if (currentDistance < smallestDistance) {
//                smallestDistance = currentDistance;
//                nearestTarget = entry.getKey();
//            }
//        }
//        return nearestTarget;
//    }
}
